/**
 * 
 */
package dt.inlee.system.action;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.common.util.TreeUtil;
import dt.inlee.domain.SysMenu;
import dt.inlee.system.service.SysMenuService;
import dt.inlee.system.vo.SysMenuBean;
import dt.inlee.system.vo.TreeNodeBean;

/**
 * @功能说明：菜单管理控制层
 * @作者： 印鲜刚
 * @创建日期： 2010-5-15
 * @
 */
@Scope("prototype")
@Results( { @Result(name = "success", type = "json") })
@Controller("sysMenuAction")
public class SysMenuAction extends ActionSupport {
	private static final long serialVersionUID = -6784977600668791997L;
	private Logger logger = Logger.getLogger(this.getClass());
	@Autowired
	private SysMenuService sysMenuService;
	private String data;
	private Map<String, String> attr;
	private List<TreeNodeBean> children;
	private String state;
	//移动菜单需要用到的参数
	private String oldNodeId, newNodeId, changeType;

	private SysMenuBean sysMenuBean = new SysMenuBean();
	//处理结果
	private boolean result = false;
	//新增节点ID值
	private String newId = null;
	//选中节点ID值
	private String selectId = null;

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public Map<String, String> getAttr() {
		return attr;
	}

	public void setAttr(Map<String, String> attr) {
		this.attr = attr;
	}

	public List<TreeNodeBean> getChildren() {
		return children;
	}

	public void setChildren(List<TreeNodeBean> children) {
		this.children = children;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public SysMenuBean getSysMenuBean() {
		return sysMenuBean;
	}

	public void setSysMenuBean(SysMenuBean sysMenuBean) {
		this.sysMenuBean = sysMenuBean;
	}

	public boolean isResult() {
		return result;
	}

	public void setResult(boolean result) {
		this.result = result;
	}

	public String getNewId() {
		return newId;
	}

	public void setNewId(String newId) {
		this.newId = newId;
	}

	public String getSelectId() {
		return selectId;
	}

	public void setSelectId(String selectId) {
		this.selectId = selectId;
	}

	public String getOldNodeId() {
		return oldNodeId;
	}

	public void setOldNodeId(String oldNodeId) {
		this.oldNodeId = oldNodeId;
	}

	public String getNewNodeId() {
		return newNodeId;
	}

	public void setNewNodeId(String newNodeId) {
		this.newNodeId = newNodeId;
	}

	public String getChangeType() {
		return changeType;
	}

	public void setChangeType(String changeType) {
		this.changeType = changeType;
	}

	/**
	 * 显示树形菜单
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "findAllSysMenus", results = { @Result(type = "json") })
	public String findAllSysMenus() throws FrameworkException, Exception {
		List<SysMenu> sysMenus = sysMenuService.findAllSysMenus();
		TreeNodeBean treeNodeBean = TreeUtil.convertMenuTreeNode(sysMenus);
		children = treeNodeBean.getChildren();
		// 父节点
		data = treeNodeBean.getData();
		attr = treeNodeBean.getAttr();
		return SUCCESS;
	}

	/**
	 * 修改菜单功能
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "modifySysMenu", results = { @Result(type = "json", params = { "excludeNullProperties", "true",
			"includeProperties", "result,newId" }) })
	public String modifySysMenu() throws FrameworkException {
		if (!BaseUtil.isEmpty(this.sysMenuService.findSysMenuByIdAndName(this.sysMenuBean.getId(), this.sysMenuBean
				.getName()))) {
			newId = "none";
			return SUCCESS;
		}
		if (BaseUtil.isEmpty(sysMenuBean.getInternalCommand())) {
			sysMenuBean.setInternalCommand("0");
		}

		logger.debug("getInternalCommand:\t" + sysMenuBean.getInternalCommand());
		SysMenu sysMenu = this.sysMenuService.findSysMenuById(this.sysMenuBean.getId());
		sysMenuBean.setOrderNo(sysMenu.getOrderNo());
		sysMenuBean.setFlag(sysMenu.getFlag());
		BeanUtils.copyProperties(this.sysMenuBean, sysMenu);
		this.sysMenuService.modifySysMenu(sysMenu);
		result = true;
		return SUCCESS;
	}

	/**
	 * 同级菜单新增功能
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "sameSaveSysMenu", results = { @Result(type = "json", params = { "excludeNullProperties", "true",
			"includeProperties", "result,newId" }) })
	public String sameSaveSysMenu() throws FrameworkException {
		if (!BaseUtil.isEmpty(this.sysMenuService.findSysMenuByName(this.sysMenuBean.getName()))) {
			newId = "none";
			return SUCCESS;
		}
		if (BaseUtil.isEmpty(sysMenuBean.getInternalCommand())) {
			sysMenuBean.setInternalCommand("0");
		}
		SysMenu sysMenu = new SysMenu();
		BeanUtils.copyProperties(this.sysMenuBean, sysMenu);
		sysMenu.setFlag("1");
		logger.debug("selectId=" + this.getSelectId());
		this.sysMenuService.sameSaveSysMenu(sysMenu, this.getSelectId());
		newId = sysMenu.getId();
		result = true;
		return SUCCESS;
	}

	/**
	 * 次级菜单新增功能
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "secondarySaveSysMenu", results = { @Result(type = "json", params = { "excludeNullProperties",
			"true", "includeProperties", "result,newId" }) })
	public String secondarySaveSysMenu() throws FrameworkException {
		if (!BaseUtil.isEmpty(this.sysMenuService.findSysMenuByName(this.sysMenuBean.getName()))) {
			newId = "none";
			return SUCCESS;
		}
		if (BaseUtil.isEmpty(sysMenuBean.getInternalCommand())) {
			sysMenuBean.setInternalCommand("0");
		}
		SysMenu sysMenu = new SysMenu();
		BeanUtils.copyProperties(this.sysMenuBean, sysMenu);
		sysMenu.setFlag("1");
		logger.debug("selectId=" + this.getSelectId());
		this.sysMenuService.secondarySaveSysMenu(sysMenu, this.getSelectId());
		newId = sysMenu.getId();
		result = true;
		return SUCCESS;
	}

	/**
	 * 菜单树删除功能
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "deleteSysMenu", results = { @Result(type = "json", params = { "excludeNullProperties", "true",
			"includeProperties", "result" }) })
	public String deleteSysMenu() throws FrameworkException {
		this.sysMenuService.deleteAllSysMenusById(this.sysMenuBean.getId());
		result = true;
		return SUCCESS;

	}

	/**
	 * 菜单树移动功能
	 * @return
	 * @throws FrameworkException
	 */
	@Action(value = "moveSysMenu", results = { @Result(type = "json", params = { "excludeNullProperties", "true",
			"includeProperties", "result" }) })
	public String moveSysMenu() throws FrameworkException {

		result = false;
		logger.debug("→→→↓↓→→拖动测试←←↓↓←←←←←");
		logger.debug("oldid:" + oldNodeId);
		logger.debug("newid:" + newNodeId);
		logger.debug("TYPE:" + changeType);
		logger.debug("→→→↑↑→→拖动测试←←↑↑←←←←←");
		// 做校验，判断变更类型
		if ("inside".equals(changeType) == false && "before".equals(changeType) == false
				&& "after".equals(changeType) == false) {
			result = false;
			return SUCCESS;
		}

		result = this.sysMenuService.moveMenu(oldNodeId, newNodeId, changeType);
		return SUCCESS;

	}
}
